package com.yanqu.road.server.cmd.banquet.crossBanquet;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.banquet.BanquetGiftInfo;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.UserCrossBanquetData;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_BANQUET_CROSS_JOIN, desc = "加入宴会")
public class JoinBanquetCmd extends AbstractCommand {

	private String banquetId;
	private int giftId;
	private int giftAcceptState;
	private int autoJoinState;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		BanquetProto.JoinBanquetReq reqMsg = BanquetProto.JoinBanquetReq.parseFrom(packet.getBytes());
		banquetId = reqMsg.getBanquetId();
		giftId = reqMsg.getGiftId();
		giftAcceptState = reqMsg.getGiftAcceptState();
		autoJoinState = reqMsg.getGiftAutoState();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		UserCrossBanquetData banquet = CrossBanquetMgr.getBanquet(banquetId);
		if(banquet == null){
			ret = GameErrorCode.E_BANQUET_IS_OVER;
			return;
		}
		if(BanquetConfigMgr.isCrossBanquet(banquet.getType()) && !SystemOpenMgr.systemOpen(player, eSystemId.BanquetCross.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(BanquetConfigMgr.isCrossMogulBirthdayBanquet(banquet.getType()) && !SystemOpenMgr.systemOpen(player, eSystemId.MogulBirthday.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		BanquetGiftInfo banquetGiftInfo = BanquetConfigMgr.getBanquetGiftInfo(banquet.getType(),giftId);
		if(banquetGiftInfo == null){
			ret = GameErrorCode.E_BANQUET_GIFT_ERROR;
			return;
		}
		if(!"0".equals(banquetGiftInfo.getJoinConsume())){
			if(player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(banquetGiftInfo.getJoinConsume()))){
				player.getModule(PreReduceModule.class).preReduce(banquetGiftInfo.getJoinConsume());
			}else{
				ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
				return;
			}
		} else {
			/**
			 * 如果被禁用 则返回系统未开放
			 */
			if (ForbiddenMgr.isForbiddenCmd(player.getUserId(), Protocol.S_BANQUET_CROSS_JOIN)) {
				ret = GameErrorCode.E_SYSTEM_NO_OPEN;
				return;
			}
		}
		CrossBanquetProto.JoinCrossBanquetReq.Builder msg = CrossBanquetMgr.joinBanquetMsg(player,banquetId,giftId);
		msg.setGiftAcceptState(giftAcceptState);
		msg.setGiftAutoState(autoJoinState);

		ActivityInfo crbAcInfo = NormalActivityMgr.getRunningCrossBanquetRankActivity();
		BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(banquet.getType());
		if (crbAcInfo == null || banquetInfo.getType1() != 5) {
			msg.setAcId(0);
		}else {
			//要知道每个宴会开的人是不是设置了自动回宴会，要去跨服走一趟
			msg.setAcId(crbAcInfo.getActivityId());
		}
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_JOIN_BANQUET, msg);
		GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			BanquetProto.JoinBanquetResp.Builder respMsg = BanquetProto.JoinBanquetResp.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_BANQUET_CROSS_JOIN, respMsg);
		}
	}
}
